package c.c.b.a.b.a.c;

import c.c.b.a.b.a.e;
import c.c.b.a.b.l;
import c.c.b.a.b.n;
import c.c.b.a.b.z.a;
import java.io.IOException;
import java.net.UnknownServiceException;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.Objects;
import javax.net.ssl.SSLSocket;

/* compiled from: ConnectionSpecSelector */
public final class b {
    public final List<n> a;
    public int b = 0;
    public boolean c;
    public boolean d;

    public b(List<n> list) {
        this.a = list;
    }

    public n a(SSLSocket sSLSocket) throws IOException {
        int i;
        n nVar;
        int size = this.a.size();
        for (i = this.b; i < size; i++) {
            nVar = (n) this.a.get(i);
            if (nVar.a(sSLSocket)) {
                this.b = i + 1;
                break;
            }
        }
        nVar = null;
        if (nVar != null) {
            boolean z;
            Object s;
            Object s2;
            for (i = this.b; i < this.a.size(); i++) {
                if (((n) this.a.get(i)).a(sSLSocket)) {
                    z = true;
                    break;
                }
            }
            z = false;
            this.c = z;
            c.c.b.a.b.a.b bVar = c.c.b.a.b.a.b.a;
            boolean z2 = this.d;
            Objects.requireNonNull((a) bVar);
            if (nVar.c != null) {
                s = e.s(l.b, sSLSocket.getEnabledCipherSuites(), nVar.c);
            } else {
                s = sSLSocket.getEnabledCipherSuites();
            }
            if (nVar.d != null) {
                s2 = e.s(e.p, sSLSocket.getEnabledProtocols(), nVar.d);
            } else {
                s2 = sSLSocket.getEnabledProtocols();
            }
            String[] supportedCipherSuites = sSLSocket.getSupportedCipherSuites();
            Comparator comparator = l.b;
            byte[] bArr = e.a;
            int length = supportedCipherSuites.length;
            int i2 = 0;
            while (i2 < length) {
                if (((l.a) comparator).compare(supportedCipherSuites[i2], "TLS_FALLBACK_SCSV") == 0) {
                    break;
                }
                i2++;
            }
            i2 = -1;
            if (z2 && i2 != -1) {
                String str = supportedCipherSuites[i2];
                int length2 = s.length + 1;
                Object obj = new String[length2];
                System.arraycopy(s, 0, obj, 0, s.length);
                obj[length2 - 1] = str;
                s = obj;
            }
            z2 = nVar.a;
            if (!z2) {
                throw new IllegalStateException("no cipher suites for cleartext connections");
            } else if (s.length != 0) {
                String[] strArr = (String[]) s.clone();
                if (!z2) {
                    throw new IllegalStateException("no TLS versions for cleartext connections");
                } else if (s2.length != 0) {
                    String[] strArr2 = (String[]) s2.clone();
                    if (strArr2 != null) {
                        sSLSocket.setEnabledProtocols(strArr2);
                    }
                    if (strArr != null) {
                        sSLSocket.setEnabledCipherSuites(strArr);
                    }
                    return nVar;
                } else {
                    throw new IllegalArgumentException("At least one TLS version is required");
                }
            } else {
                throw new IllegalArgumentException("At least one cipher suite is required");
            }
        }
        StringBuilder g = c.b.a.a.a.g("Unable to find acceptable protocols. isFallback=");
        g.append(this.d);
        g.append(", modes=");
        g.append(this.a);
        g.append(", supported protocols=");
        g.append(Arrays.toString(sSLSocket.getEnabledProtocols()));
        throw new UnknownServiceException(g.toString());
    }
}
